package com.wva.video.controller;

import com.wva.common.annotation.authority.Open;
import com.wva.common.context.UserContext;
import com.wva.common.utils.R;
import com.wva.video.domain.vo.VideoInteractionDataVO;
import com.wva.video.service.VideoCollectService;
import com.wva.video.service.VideoInteractionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author: hong
 * @Date: 2025/2/16 00:00:24
 */
@Api(tags = "视频互动相关接口")
@RequestMapping("/video/interaction")
@RestController
public class InteractionController {
    @Resource
    VideoInteractionService videoInteractionService;
    @Resource
    VideoCollectService videoCollectService;

    /**
     * 互动数据包括播放量、点赞量、转发量、当前用户是否点过赞
     * @param videoId
     * @return
     */
    @ApiOperation("获取视频互动数据")
    @GetMapping("/{videoId}")
    @Open
    R<VideoInteractionDataVO> getInteractionData(@PathVariable Long videoId) {
        return R.ok(
                VideoInteractionDataVO.builder()
                        .likeCount(videoInteractionService.getVideoLikeCountByVideoId(videoId))
                        .like(UserContext.isLogin() ? videoInteractionService.isLikeVideoByVideoId(videoId) : null)
                        .playCount(0L)
                        .collectCount(videoCollectService.getVideoCollectCountByVideoId(videoId))
                        .collect(UserContext.isLogin() ? videoCollectService.isCollectVideoByVideoId(videoId) : null)
                        .repostCount(0L)
                        .build()
        );
    }

    @ApiOperation("点赞视频")
    @PostMapping("/{videoId}/like")
    R<Object> like(@PathVariable Long videoId) {
        videoInteractionService.likeVideo(videoId);
        return R.ok();
    }

    @ApiOperation("取消点赞视频")
    @PostMapping("/{videoId}/unlike")
    R<Object> unlike(@PathVariable Long videoId) {
        videoInteractionService.unlikeVideo(videoId);
        return R.ok();
    }

    @ApiOperation("收藏视频")
    @PostMapping("/{videoId}/collect")
    R<Object> collect(@PathVariable Long videoId) {
        videoCollectService.collect(videoId);
        return R.ok();
    }

    @ApiOperation("取消收藏视频")
    @PostMapping("/{videoId}/cancel-collect")
    R<Object> cancelCollect(@PathVariable Long videoId) {
        videoCollectService.cancelCollect(videoId);
        return R.ok();
    }
}
